APP_EVENTS.AUTHENTICATION_REQUIRED = "authenticationRequired";
APP_EVENTS.AUTHENTICATION_ERROR = "authenticationError";
APP_EVENTS.REQUEST_ERROR = "requestError";

Ext.define('Util.Backend', {}); // Placeholder, ignore this.

window.Backend =
{
    GET : 'GET',
    POST : 'POST',

    request : function(method, url, params, callbacks)
    {
        var me = this;

        var failureFn = function(resp)
        {
        	debugger
            if (callbacks.failure)
            {
                var response = Ext.decode(resp.responseText, true);
                callbacks.failure(response, resp);
                if (!!response && !!response.response)
                {
                    Log.error(response.response.message);
                }
            } else
            {
                me.handleErrorResponse(resp);
            }
        };

        var successFn = function(response)
        {
        	debugger
            var resp = Ext.decode(response.responseText, true);
            if (resp)
            {
                if (!resp.success && resp.response && resp.response.authenticationRequired)
                {
                    window.location = "login?redirectURL=/";
                    //                        APP.fireEvent(APP_EVENTS.AUTHENTICATION_REQUIRED);
                } else
                {
                    if (callbacks.success && resp.success)
                    {
                        callbacks.success(resp.response);
                    }
                    if (!resp.success)
                    {
                        APP.fireEvent(APP_EVENTS.REQUEST_ERROR, resp.response);
                        failureFn(response);
                    }
                }
            } else
            {
            	debugger
                APP.fireEvent(APP_EVENTS.REQUEST_ERROR,
                {
                    message : "Invalid JSON response"
                });
                failureFn(response);
            }
        };

        Ext.Ajax.request(
        {
            url : url,
            method : method,
            params : params,
            success : successFn,
            failure : failureFn
        });
    },

    handleErrorResponse : function(response)
    {
        if (response && (response.status == "403" || response.status == "401"))
        {
            APP.fireEvent(APP_EVENTS.AUTHENTICATION_ERROR,
            {
                message : response.statusText
            });
        } else
        {
            var message = "Unknown error";
            if (response && response.statusText)
            {
                message = response.statusText;
            }
            APP.fireEvent(APP_EVENTS.REQUEST_ERROR,
            {
                message : message
            });
        }
    },

    login : function(username, password, callback)
    {
        var me = this;
        Ext.Ajax.request(
        {
            url : me.serviceUrl + 'login',
            method : 'POST',
            params :
            {
                username : username,
                password : password,
                submit : "Login"
            },
            success : function(response)
            {
                var resp = Ext.decode(response.responseText, true);
                if (resp && resp.success && callback)
                {
                    callback();
                } else if (resp && !resp.success && resp.response)
                {
                    APP.fireEvent(APP_EVENTS.AUTHENTICATION_ERROR, resp.response);
                }
            },
            failure : me.handleErrorResponse
        });
    },

    logout : function()
    {
        var me = this;
        me.request("POST", "logout", {});
    },

    requestJSOP : function(url, params, callbackKey, callBackFunction)
    {
        Ext.data.JsonP.request(
        {
            url : url,
            callbackKey : callbackKey || 'callback',
            params : params,
            success : function(result, request)
            {
            //callBackFunction(result);
            },
            failure : function(result, request)
            {}
        });
    }
};